package com.chenqi.tool.dialog

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chenqi.tool.R
import com.chenqi.tool.recycleview.base.BaseRecycleViewAdapter
import com.chenqi.tool.recycleview.base.OnItemClickListener

/**
 * 作者: CQ
 * 日期: 2021-06-11
 * 说明:
 */
class FootListDialog(context: Context, themeResId: Int) : Dialog(context, themeResId) {

    init {
        // 拿到Dialog的Window, 修改Window的属性,获取Window的LayoutParams一定要重新设置, 才能生效
        window!!.decorView.setPadding(0, 0, 0, 0)
        val attributes = window!!.attributes
        attributes.width = ViewGroup.LayoutParams.MATCH_PARENT
        attributes.gravity = Gravity.BOTTOM
        window!!.attributes = attributes
    }

    class Builder(private var context: Context?) {
        private var list: MutableList<String>? = null
        private var onItemClick: OnItemClickListener? = null
        private var isCancelable = true
        private var isCancelOutside = true

        fun setData(list: MutableList<String>): Builder {
            this.list = list
            return this
        }

        fun setOnItemClickListener(onItemClick: OnItemClickListener): Builder {
            this.onItemClick = onItemClick
            return this
        }

        /**
         * 设置是否可以按返回键取消
         *
         * @param isCancelable
         * @return
         */
        fun setCancelable(isCancelable: Boolean): Builder {
            this.isCancelable = isCancelable
            return this
        }

        /**
         * 设置是否可以取消
         *
         * @param isCancelOutside
         * @return
         */
        fun setCancelOutside(isCancelOutside: Boolean): Builder {
            this.isCancelOutside = isCancelOutside
            return this
        }


        fun create() {
            val view: View = LayoutInflater.from(context).inflate(R.layout.foot_list_dialog, null)

            val bottomListDialog = FootListDialog(context!!, R.style.ActionSheetDialogStyle)

            val recyclerView = view.findViewById<RecyclerView>(R.id.foot_popup_recyclerView)
            val linearLayoutManager = LinearLayoutManager(context)
            linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
            linearLayoutManager.isAutoMeasureEnabled = true

            val dividerItemDecoration =
                DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
            dividerItemDecoration.setDrawable(
                ContextCompat.getDrawable(
                    context!!, R.drawable.my_divider
                )!!
            )
            recyclerView.addItemDecoration(dividerItemDecoration)

            recyclerView.layoutManager = linearLayoutManager // 设置线行垂直布局
            val footPopupAdapter = FootListDialogAdapter(context!!, list!!)
            recyclerView.adapter = footPopupAdapter

            view.findViewById<TextView>(R.id.foot_popup_bottom)
                .setOnClickListener { bottomListDialog.dismiss() }

            footPopupAdapter.setOnItemClickListener(object : OnItemClickListener {
                override fun onItemClickListener(v: View?, position: Int) {
                    bottomListDialog.cancel()
                    onItemClick!!.onItemClickListener(v, position)
                }
            })// 设置监听

            bottomListDialog.setContentView(view)
            // dialog弹出后会点击屏幕或物理返回键，dialog不消失
            bottomListDialog.setCancelable(isCancelable)
            // 点击Dialog以外的区域时Dialog消失
            bottomListDialog.setCanceledOnTouchOutside(isCancelOutside)
            // 显示
            bottomListDialog.show()
        }

        fun <T> create(adapter: T) {
            if (adapter is BaseRecycleViewAdapter<*>) {
                val view: View =
                    LayoutInflater.from(context).inflate(R.layout.foot_list_dialog, null)
                val bottomListDialog = FootListDialog(context!!, R.style.ActionSheetDialogStyle)

                val recyclerView = view.findViewById<RecyclerView>(R.id.foot_popup_recyclerView)
                val linearLayoutManager = LinearLayoutManager(context)
                linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
                linearLayoutManager.isAutoMeasureEnabled = true

                val dividerItemDecoration =
                    DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
                dividerItemDecoration.setDrawable(
                    ContextCompat.getDrawable(
                        context!!, R.drawable.my_divider
                    )!!
                )
                recyclerView.addItemDecoration(dividerItemDecoration)

                recyclerView.layoutManager = linearLayoutManager // 设置线行垂直布局
                recyclerView.adapter = adapter

                view.findViewById<TextView>(R.id.foot_popup_bottom)
                    .setOnClickListener { bottomListDialog.dismiss() }

                adapter.setOnItemClickListener(object : OnItemClickListener {
                    override fun onItemClickListener(v: View?, position: Int) {
                        bottomListDialog.cancel()
                        onItemClick!!.onItemClickListener(v, position)
                    }
                })// 设置监听

                bottomListDialog.setContentView(view)
                // dialog弹出后会点击屏幕或物理返回键，dialog不消失
                bottomListDialog.setCancelable(isCancelable)
                // 点击Dialog以外的区域时Dialog消失
                bottomListDialog.setCanceledOnTouchOutside(isCancelOutside)
                // 显示
                bottomListDialog.show()
            }
        }
    }

}